<?php

namespace app\api\controller\hotel;

use app\common\controller\Api;
use think\Hook;
use think\exception\DbException;
use think\exception;

/**
 * 获取数据
 */
class Init extends Api
{
    protected $noNeedLogin = '*';
    protected $noNeedRight = '*';
    
    protected $stub_type = 'table';

    public function _initialize()
    {
        parent::_initialize();
    }
    
    /**
     * 
     */
    public function start(){
        $data = [
            'id'=>1,'name'=>'你好','text'=>'采访时登录考放假了水电费精灵盛典水电费记录卡水电费精灵盛典'
        ];
        $this->createTableByData($data);
    }

        /**
     * 根据链接获取数据
     */
    public function getData(){
        
    }
    
    
    /**
     * 根据链接获取数据
     */
    public function createTableByData($data,$table){
        if($this->check_table(config('database.prefix') .$table)){ //表是否存在
            return true;
        }
        
        $field_list = [];
        foreach ($data as $key=>$value){
            $value = trim($value);
            $field_info = db('a_field')->where('field',$key)->find();
            if(empty($field_info)) {
                $stub_item = ['field_name'=> strtolower($key),'comment'=>'暂无'];
                $type = 'string';
            }else{
                $stub_item = ['field_name'=>strtolower($field_info['field']),'comment'=>$field_info['beizhu']];
                $type = 'string';
            }
            
            
            $field_list[] = $this->getReplacedStub(strtolower($type),$stub_item);   
        }
        $data  = ['table_name'=>config('database.prefix').$table,'field_list'=> implode("\r\n", $field_list)];
        $content = $this->getReplacedStub($this->stub_type,$data);   
       
        try{
            db()->query($content);
            return true;
        } catch (exception $e){
            return $e->getMessage();
        }
        
    }
    
    public function check_table($table){
        $res = db()->query('SHOW TABLES LIKE '."'".$table."'");
        if($res){
            return 1;
        }else{
            return 0;
        }
    }
    
    
    public function get_table_field($table){
        $field_list  = db()->query('SHOW FULL COLUMNS FROM `' . $table . '`');
        $field_key = [];
        foreach ($field_list as $value){
            $field_key[] = $value['Field'];
        }
        return $field_key;
    }
    
    
    /**
     * 获取替换后的数据
     * @param string $name
     * @param array  $data
     * @return string
     */
    protected function getReplacedStub($name, $data)
    {
        foreach ($data as $index => &$datum) {
            $datum = is_array($datum) ? '' : $datum;
        }
        unset($datum);
        $search = $replace = [];
        foreach ($data as $k => $v) {
            $search[] = "{%{$k}%}";
            $replace[] = $v;
        }
        $stubname = $this->getStub($name);
        
        $stub = file_get_contents($stubname);
        
        $content = str_replace($search, $replace, $stub);
        return $content;
    }
    
    
    protected function getStubDir(){
        $module = request()->module();
        return APP_PATH  . DS .$module . DS. 'stubs' . DS . $this->stub_type .DS;
    }


    /**
     * 获取基础模板
     * @param string $name
     * @return string
     */
    protected function getStub($file)
    {
        $suffix = '.stub';
        $dir = $this->getStubDir();
        $file = empty($file) ? $this->stub_type :$file;       
        if(!is_file($dir .$file .$suffix)){
            $dir  = $dir .'stubs' .DS;
        }
        return $dir .$file . $suffix;
    }
   
    
       

    
}
